package com.woniu.order.mq;

import com.woniu.order.client.GymServerClient;
import com.woniu.order.dao.TInOrderMapper;
import com.woniu.order.dto.PayInfoDto;
import com.woniu.order.model.TInOrder;
import com.woniu.order.util.AlipayUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Component
@RabbitListener(queues = "check_gym_order_queue")
public class CheckOrderConsumer {
    @Resource
    private AlipayUtil alipayUtil;
    @Resource
    private TInOrderMapper inOrderMapper;

   @Resource
   private GymServerClient gymServerClient;

    @GlobalTransactional(rollbackFor = Exception.class)
    @RabbitHandler
    public void handler(String orderNum)throws Exception{
        //主动的去调用支付宝接口查询支付状态
        PayInfoDto dto = alipayUtil.queryPayStatus(orderNum);
        TInOrder orderInfo = inOrderMapper.selectById(orderNum);

        System.out.println("状态"+dto.getPayState());
        if(dto.getPayState().equals("TRADE_SUCCESS")){
            if (orderInfo.getOrderState()== 0){
                orderInfo.setOrderState(1);
                LocalDateTime localDate=LocalDateTime.parse(dto.getPayDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                orderInfo.setOrderPaytime(localDate);
                // 修改场馆的订单状态为已支付，店铺状态为正常
              gymServerClient.modifyGymState(orderInfo.getGymNum());

            }
        }
    }
}
